package hadoop.chp06.v17024410134;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HBaseDemo63 {
    public static void main(String[] args)throws Exception{


        createTable();
        insert1();
        insert2();
        insert3();
        insert4();
        insert5();
        insert6();
        insert5();
        scan();
        get("Rain","info","birthday");

   /*    insert("Rain",tableName,column1,value1,column2,value2,column3,value3);
       get(tableName,"Rain");
        scan();*/
    }
    static Configuration conf = HBaseConfiguration.create();
    private  static void createTable() throws Exception{

        conf.set("hbase.zookeeper.quorum", "node1");
        conf.set("hbase.rootdir", "hdfs://node1:9000/hbase");
        conf.set("hbase.cluster.distributed", "true");
        Connection connect = ConnectionFactory.createConnection(conf);
        Admin admin = connect.getAdmin();
        TableName tn = TableName.valueOf("emp17024410134");
        String[] family=new String[]
        {"member_id","address","info"};
        //System.out.println("table Exists!");
        HTableDescriptor desc = new HTableDescriptor(tn);
        for (int i = 0; i < family.length; i++) {
            desc.addFamily(new HColumnDescriptor(family[i]));

        }
        if (admin.tableExists(tn)) {
            System.out.println("table Exists!");
            System.exit(0);
        } else {
            admin.createTable(desc);
            System.out.println("create table Success!");
        }


    }
    private static void insert1() throws Exception {
        conf.set("hbase.zookeeper.quorum", "192.168.0.103");
        HTable table = new HTable(conf, "emp17024410134");
        Put put = new Put(Bytes.toBytes("Rain"));
        put.addColumn(Bytes.toBytes("info"),      //列族的名字
                Bytes.toBytes("age"),   //列的名字
                Bytes.toBytes("28"));       //值
        table.put(put);
        table.close();
        System.out.println("插入成功");

    }
    private static void insert2() throws Exception {
        conf.set("hbase.zookeeper.quorum", "192.168.0.103");
        HTable table = new HTable(conf, "emp17024410134");
        Put put = new Put(Bytes.toBytes("Rain"));
        put.addColumn(Bytes.toBytes("info"),      //列族的名字
                Bytes.toBytes("birthday"),   //列的名字
                Bytes.toBytes("1990-05-01"));       //值
        table.put(put);
        table.close();
        System.out.println("插入成功2");

    }
    private static void insert3() throws Exception {
        conf.set("hbase.zookeeper.quorum", "192.168.0.103");
        HTable table = new HTable(conf, "emp17024410134");
        Put put = new Put(Bytes.toBytes("Rain"));
        put.addColumn(Bytes.toBytes("info"),      //列族的名字
                Bytes.toBytes("industry"),   //列的名字
                Bytes.toBytes("architect"));       //值
        table.put(put);
        table.close();
        System.out.println("插入成功3");

    }
    private static void insert4() throws Exception {
        conf.set("hbase.zookeeper.quorum", "192.168.0.103");
        HTable table = new HTable(conf, "emp17024410134");
        Put put = new Put(Bytes.toBytes("Rain"));
        put.addColumn(Bytes.toBytes("address"),      //列族的名字
                Bytes.toBytes("country"),   //列的名字
                Bytes.toBytes("China"));       //值
        table.put(put);
        table.close();
        System.out.println("插入成功4");

    }
    private static void insert5() throws Exception {
        conf.set("hbase.zookeeper.quorum", "192.168.0.103");
        HTable table = new HTable(conf, "emp17024410134");
        Put put = new Put(Bytes.toBytes("Rain"));
        put.addColumn(Bytes.toBytes("address"),      //列族的名字
                Bytes.toBytes("city"),   //列的名字
                Bytes.toBytes("ShenZhen"));       //值
        table.put(put);
        table.close();
        System.out.println("插入成功5");

    }
    private static void insert6() throws Exception {
        conf.set("hbase.zookeeper.quorum", "192.168.0.103");
        HTable table = new HTable(conf, "emp17024410134");
        Put put = new Put(Bytes.toBytes("Rain"));
        put.addColumn(Bytes.toBytes("member_id"),      //列族的名字
                Bytes.toBytes("id"),   //列的名字
                Bytes.toBytes("31"));       //值
        table.put(put);
        table.close();
        System.out.println("插入成功6");

    }







    private static void scan() throws Exception {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.0.103");

        //指定表的客户端
        HTable table = new HTable(conf, "emp17024410134");

        //创建一个扫描器 Scan
        Scan scanner = new Scan(); //----> 相当于: select * from students;
        //scanner.setFilter(filter)  ----> 过滤器

        //执行查询
        ResultScanner rs = table.getScanner(scanner); //返回ScannerResult ---> Oracle中的游标
        for(Result r:rs){
            String id = Bytes.toString(r.getValue(Bytes.toBytes("member_id"), Bytes.toBytes("id")));
            String age = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")));
            String birthday = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("birthday")));
            String industry = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("industry")));
            String city = Bytes.toString(r.getValue(Bytes.toBytes("address"), Bytes.toBytes("city")));
            String country = Bytes.toString(r.getValue(Bytes.toBytes("address"), Bytes.toBytes("country")));
            System.out.println("您所查询结果为："+"id:"+id+"       " +"age:"+age + "   "+"birthday:"+ birthday+ "   "+"industry:"+industry+ "   "+"city:"+city+ "   "+"country:"+country);
        }
        table.close();
    }
    private static void get(String Rolkey,String liezu,String column )throws Exception  {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.0.103");

        //指定表的客户端
        HTable table = new HTable(conf, "emp17024410134");

        //通过Get查询
        Get get = new Get(Bytes.toBytes(Rolkey));
        //执行查询
        Result record = table.get(get);
       // System.out.println(Rolkey+" "+ liezu);



        //输出
        String result = Bytes.toString(record.getValue(Bytes.toBytes(liezu), Bytes.toBytes(column)));
        System.out.println("通过行键Rain，列族info，列birthday,查询结果如下所示");
        System.out.println(result);

        table.close();
    }


}
